﻿namespace Yom.WebService.Admin
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using Yom.Common;

    public class AdminChapter : AdminBase
    {
        public void OutWrite()
        {
            string Action = Factory.Request("Action");
            string VID = Factory.Request("VID");
            string ID = Factory.Request("ID");
            string ImgPath = Factory.Request("ImgPath");
            string OrderID = Factory.Request("OrderID");
            string Mod = Factory.Request("Mod");
            StringBuilder builder;

            switch (Action)
            {
                case "Edit":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        Yom.Model.Chapter model = new Yom.Data.Chapter().GetModel(ID.ToLong());

                        if (Mod != "1")
                        {
                            builder = new StringBuilder();
                            builder.Append("<script type=\"text/javascript\" src=\"Images/Js/jquery.uploadify.js\"></script>");
                            builder.Append("<div style=\"text-align:center\"><img id=\"imgShow\" src=\"" + model.ImgPath + "\" border=\"0\" class=\"imgShow\" alt=\"\" /></div><br />");
                            builder.Append("<input type=\"file\" id=\"photo\" name=\"photo\" /><br />");
                            builder.Append("排序编号: <input type=\"text\" class=\"txt\" id=\"orderid\" value=\"" + model.OrderID.ToString() + "\" /><br />");
                            builder.Append("<div id=\"ErrShow\" style=\"color:red\"></div><div class=\"SubButton\"><input type=\"button\" id=\"Submit\" value=\"修改图片\" class=\"button\" /></div>");
                            base.Response.Write(builder.ToString());
                        }
                        else
                        {
                            string old = model.ImgPath;
                            if (!string.IsNullOrEmpty(ImgPath) && ImgPath != "undefined")
                                model.ImgPath = ImgPath;
                            model.OrderID = ((!string.IsNullOrEmpty(OrderID)) ? OrderID.ToInt() : 0);
                            if (new Yom.Data.Chapter().Update(model))
                            {
                                if (!string.IsNullOrEmpty(ImgPath) && ImgPath != "undefined")
                                {
                                    try { Fso.DeleteFile(old); }
                                    catch (Exception ex) { }
                                }
                                base.Response.Write("1");
                            }
                            else
                                base.Response.Write("0");
                        }
                    }
                    break;
                case "DelImg":
                    try
                    {
                        Fso.DeleteFile(ImgPath);
                    }
                    catch (Exception ex) { }
                    break;
                case "Del":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        if (new Yom.Data.Chapter().Delete(ID.ToLong()))
                        {
                            string path = new Yom.Data.Chapter().GetModel(ID.ToLong()).ImgPath;
                            try
                            {
                                Fso.DeleteFile(path);
                            }
                            catch (Exception ex) { }

                            base.Response.Write("1");
                        }
                        else
                            base.Response.Write("0");
                    }
                    break;
                default:
                    if (!string.IsNullOrEmpty(VID))
                    {
                        builder = new StringBuilder();
                        string P = Factory.Request("P");
                        int page = (string.IsNullOrEmpty(P) ? 1 : ((P == "0") ? 1 : P.ToInt()));
                        builder.Append("<div class=\"SubMenu\"><a href=\"javascript:$.Run(7,0,0," + VID + ")\">章节管理</a> | <a href=\"javascript:$.Run(6,4,0," + new Yom.Data.Volume().GetModel(VID.ToLong()).ComicID.ToString() + "," + VID + ")\">添加章节</a></div>");
                        builder.Append("<div align=\"left\" style=\"padding:5px;\">");
                        builder.Append("<table border=\"0\">");
                        builder.Append("<tr><td width=\"300\" valign=\"top\"><table border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"TablePanel\">");
                        builder.Append("<tr align=\"center\" class=\"TDHead\"><td nowrap>章节图片</td><td nowrap>章节序号</td><td nowrap>&nbsp;</td></tr>");
                        List<Yom.Model.Chapter> list = new Yom.Data.Chapter().GetList(5, page, VID.ToLong(), true);
                        foreach (Yom.Model.Chapter model in list)
                        {
                            builder.Append("<tr><td align=\"center\" valign=\"middle\">");
                            builder.AppendFormat("<a href=\"javascript:$.Run(7,1,{0},{1},{2})\"><img id=\"img{2}\" border=\"0\" width=\"100\" height=\"100\" onmousemove=\"$.Run(7,3,{2})\" src=\"{3}\" alt=\"\" /></a></td>", page, VID, model.ChapterID, model.ImgPath);
                            builder.AppendFormat("<td align=\"center\" valign=\"middle\">{0}</td><td align=\"center\" valign=\"middle\"><img src=\"Images/Admin/del.gif\" style=\"cursor:pointer\" onclick=\"$.Run(7,2,{1},{2},{3})\" border=\"0\" width=\"16\" height=\"16\" alt=\"\" /></td>", model.OrderID, page, VID, model.ChapterID);
                            builder.Append("</tr>");
                        }
                        builder.Append("</table></td><td align=\"center\" valign=\"middle\"><img id=\"photoShow\" class=\"photoShow\" src=\"Images/Common/Wait.gif\" border=\"0\" alt=\"图片预览\" /></td></tr>");
                        builder.Append("</table><br /><div id=\"ShowMsg\" style=\"color:red\"></div>");
                        builder.Append("<div class=\"more\">" + Factory.GetPager(5, page, new Yom.Data.Chapter().GetCount(VID.ToLong()), "javascript:$.Run(7,0,<{Page}>," + VID + ")") + "</div>");
                        base.Response.Write(builder.ToString());
                    }
                    break;
            }
        }
    }
}